package cn.fl.insurance.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsPlcApprQueryCondition;
import cn.fl.insurance.dto.InsPlcApprDTO;
import cn.fl.insurance.service.IInsPlcApprApiService;
import cn.fl.insurance.service.IInsPlcApprService;
import cn.fl.insurance.transform.InsPlcApprVOTransform;
import cn.fl.insurance.transform.InsPlcBscInfoVOTransform;
import cn.fl.insurance.vo.InsPlcApprVO;
import cn.fl.insurance.vo.InsPlcBscInfoVO;

/**
 * @Description: 批单表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsPlcApprServiceImpl implements IInsPlcApprService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsPlcApprServiceImpl.class);

    /**
     * TODO 自定义 API 暴露服务
     */
    @Autowired
    private IInsPlcApprApiService insPlcApprApiService;

    @Override
    public InsPlcApprVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsPlcApprDTO dto = this.insPlcApprApiService.selectInsPlcApprById(id);
        return InsPlcApprVOTransform.toVO(dto);
    }

    @Override
    public Long add(InsPlcApprVO entity) throws BaseException {
        if (null == entity) return null;
        return this.insPlcApprApiService.addInsPlcAppr(InsPlcApprVOTransform.toDTO(entity));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Override
    public Boolean add(InsPlcBscInfoVO entity) throws BaseException {
        return this.insPlcApprApiService.addInsPlcAppr(InsPlcBscInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insPlcApprApiService.removeInsPlcApprByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsPlcApprVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insPlcApprApiService.modifyInsPlcAppr(InsPlcApprVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        return this.insPlcApprApiService.searchInsPlcApprListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    @Override
    public List<InsPlcApprVO> searchList(InsPlcApprQueryCondition condition) throws BaseException {
        return InsPlcApprVOTransform.toVOList(this.insPlcApprApiService.searchInsPlcApprList(condition));
    }

    /**
     * 查询当前保单的批单最后一条记录
     *
     * @param plcId
     * @return
     */
    @Override
    public InsPlcApprDTO selectTopOneByPlcId(Long plcId) {
        return this.insPlcApprApiService.selectTopOneByPlcId(plcId,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询当前批单的批单最后一条记录
     *
     * @param apprId
     * @return
     */
    @Override
    public InsPlcApprDTO selectLastByApprId(Long apprId) {
        return this.insPlcApprApiService.selectLastByApprId(apprId,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 验证批单号是否唯一
     *
     * @param apprCode
     * @return
     * @throws BaseException
     */
    @Override
    public boolean validateApprCode(String apprCode) throws BaseException {
        InsPlcApprQueryCondition condition=new InsPlcApprQueryCondition();
        //condition.setApprCode(apprCode);
        condition.setApprCodeUnique(apprCode);
        List<InsPlcApprDTO> list=this.insPlcApprApiService.searchInsPlcApprList(condition);
        if(CollectionUtils.isNotEmpty(list)){
            return true;
        }
        return false;
    }
}
